<?php

namespace App\Admin\Controllers;

use App\Models\Ad;

use App\Models\AdPosition;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class AdController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('广告管理');
            $content->description('');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑广告');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('添加广告');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Ad::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column('pic','图片')->image('', '',20);
            $grid->ad_position()->ad_nick('广告位');
            $grid->column('name','广告名称')->editable();
            $grid->column('pic_style','图片样式');
            $grid->status('状态')->switch(Ad::adStatus());

            $grid->created_at();
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Ad::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->select('ad_position_name','广告位')->options(AdPosition::all()->pluck('ad_nick', 'ad_name'));
            $form->text('name','广告名称')->rules('required');
            $form->text('href','广告名称链接');
            $form->image('pic','广告图片')->uniqueName()->rules('required');;
            $form->select('pic_style','广告图片样式')->options(Ad::adStyle())->help('样式请到七牛配置')->setWidth(4);
            $form->switch('status','状态')->states(Ad::adStatus());
            $form->number('order','排序')->help('数值越大，排名越靠前')->default(1);

            //$form->editor('content')->setPicStyle('!w600')->attribute(['style'=>'height:400px;max-height:500px;']);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
